# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2010  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4 import QtCore
from PyQt4.QtGui import *

from sys import path
path.append("../")

import aeDatabase as database

from ui_visualWindow import Ui_VisualWindow

from pathwayScene import PathwayScene
from reactionVisual import ReactionVisual
from compoundVisual import CompoundVisual
from filterTabWidget import FilterFrame
from reactionDetails import ReactionDetailsWidget

class VisualWindow(QMainWindow):
	def __init__(self, parent, controller):
		QMainWindow.__init__(self, parent)
		
		self.controller = controller

		self.ui = Ui_VisualWindow()
		self.ui.setupUi(self)

		self.setupCentralWidget()
		self.setupMenus()
		
		self.progressBar = QProgressBar()
		self.statusBar().addWidget(self.progressBar)

		r2 = database.Reaction("aghvcmlnaW5kYnIQCxIIUmVhY3Rpb24Yi7wBDA")
		r = database.Reaction("aghvcmlnaW5kYnIPCxIIUmVhY3Rpb24Y_G0M")
		r3 = database.Reaction("aghvcmlnaW5kYnIQCxIIUmVhY3Rpb24Yk-MBDA")

		rVis2 = ReactionVisual(r2)
		rVis = ReactionVisual(r)
		rVis3 = ReactionVisual(r3)
		rVis.setPos(250,50)
		rVis2.setPos(0,100)

		self.pathwayScene.addItem(rVis2)
		self.pathwayScene.addItem(rVis3)
		self.pathwayScene.addItem(rVis)
		rVis.updateCompounds()

		self.connect(self.ui.actionNew_Pathway, QtCore.SIGNAL("triggered(bool)"), self.blank)
		self.connect(self.ui.actionSave_Pathway, QtCore.SIGNAL("triggered(bool)"), self.blank)
		self.connect(self.ui.actionLoad_Pathway, QtCore.SIGNAL("triggered(bool)"), self.blank)
		self.connect(self.ui.actionMove, QtCore.SIGNAL("triggered(bool)"), self.blank)
		self.connect(self.ui.actionConnection, QtCore.SIGNAL("triggered(bool)"), self.blank)
		self.connect(self.ui.actionEraser, QtCore.SIGNAL("triggered(bool)"), self.blank)

	def sizeHint(self):
		return QtCore.QSize(800,800)

	def blank(self, b):
		pass

	def setupCentralWidget(self):
		self.reactionDetails = ReactionDetailsWidget()

		self.resultsScene = QGraphicsScene()
		self.resultsView = QGraphicsView(self.resultsScene)

		self.filterFrame = FilterFrame(self.resultsScene)

		self.pathwayScene = PathwayScene()
		self.pathwayView = QGraphicsView(self.pathwayScene)
		self.pathwayScene.reactionDetails = self.reactionDetails

		self.searchFrame = QFrame()
		self.searchFrame.setFrameShape(QFrame.StyledPanel)
		self.searchFrame.setFrameShadow(QFrame.Raised)
		self.searchLayout = QHBoxLayout()
		self.searchSplitter = QSplitter()
		self.searchSplitter.addWidget(self.filterFrame)
		self.searchSplitter.addWidget(self.resultsView)
		self.searchLayout.addWidget(self.searchSplitter)

		self.searchFrame.setLayout(self.searchLayout)

		self.horizSplitter = QSplitter(QtCore.Qt.Horizontal)
		self.vertSplitter = QSplitter(QtCore.Qt.Vertical)

		self.horizSplitter.addWidget(self.reactionDetails)
		self.horizSplitter.addWidget(self.searchFrame)

		self.vertSplitter.addWidget(self.pathwayView)
		self.vertSplitter.addWidget(self.horizSplitter)

		self.setCentralWidget(self.vertSplitter)

		self.vertSplitter.setSizes([400, 400])
		self.horizSplitter.setSizes([300,500])
		self.searchSplitter.setSizes([300,200])

	def setupMenus(self):
		self.toolGroup = QActionGroup(self)
		self.toolGroup.setExclusive(True)

		self.actionConnection = QAction("Connection Tool", self)
		self.actionConnection.setCheckable(True)
		icon = QIcon()
		icon.addPixmap(QPixmap(":/images/draw-freehand.png"), QIcon.Normal, QIcon.Off)
		self.actionConnection.setIcon(icon)
		self.actionConnection.setActionGroup(self.toolGroup)

		self.actionEraser = QAction("Eraser Tool", self)
		self.actionEraser.setCheckable(True)
		icon1 = QIcon()
		icon1.addPixmap(QPixmap(":/images/draw-eraser.png"), QIcon.Normal, QIcon.Off)
		self.actionEraser.setIcon(icon1)
		self.actionEraser.setActionGroup(self.toolGroup)

		self.actionMove = QAction("Move Tool", self)
		self.actionMove.setCheckable(True)
		icon2 = QIcon()
		icon2.addPixmap(QPixmap(":/images/transform-move.png"), QIcon.Normal, QIcon.Off)
		self.actionMove.setIcon(icon2)
		self.actionMove.setActionGroup(self.toolGroup)

		self.ui.menuTools.addAction(self.actionConnection)
		self.ui.menuTools.addAction(self.actionMove)
		self.ui.menuTools.addAction(self.actionEraser)

		self.ui.toolBar.addAction(self.actionConnection)
		self.ui.toolBar.addAction(self.actionMove)
		self.ui.toolBar.addAction(self.actionEraser)
